﻿$(document).ready(function() {
    var feedbackSent = false;
    $('#submit').click(function() {
        if (!feedbackSent && $('#respondFeedbackTextArea').val().length != 0) {
            feedbackSent = true;
            $.ajax({
                type: 'POST',
                url: "/Respond/SaveFeedback",
                data: { questionID: $('#questionID').val(), comment: $('#respondFeedbackTextArea').val() }
            });
        }
        $.ajax({
            type: 'POST',
            url: "/Respond/DeleteNumericResponse",
            data: { questionID: $('#questionID').val() },
            async: false
        });
        if ($('#respondNumericAnswer').val().length > 0) {
            if (isNaN($('#respondNumericAnswer').val())) {
                $('#respondError').text("Your answer must be a number");
                $('#submit').blur(function() {
                    $('#respondError').text("");
                    $('#submit').unbind('blur');
                });
            }
            else {
                $.ajax({
                    type: 'POST',
                    url: "/Respond/SaveNumericResponse",
                    data: { questionID: $('#questionID').val(), answer: $('#respondNumericAnswer').val() },
                    success: function() {
                        $('#submit').val("Sent");
                        $('#submit').attr("disabled", true);
                    }
                });
                $('#submit').val("Sent");
                $('#submit').attr("disabled", true);
                $('#respondNumericAnswer').keyup(function() {
                    $('#submit').attr("disabled", false);
                    $('#submit').val("Submit");
                    $('#respondNumericAnswer').unbind('keyup');
                });
            }
        }
        else {
            $('#respondError').text("You have not provided an answer");
            $('#submit').blur(function() {
                $('#respondError').text("");
                $('#submit').unbind('blur');
            });
        }
    });
});